package chapter21.chapter21_9;//: concurrency/ListComparisons.java
// {Args: 1 10 10} (Fast verification check during build)
// Rough comparison of thread-safe List performance.
import java.util.concurrent.*;
import java.util.*;
import net.mindview.util.*;

abstract class ListTest extends Tester<List<Integer>> {
  ListTest(String testId, int nReaders, int nWriters) {
    super(testId, nReaders, nWriters);
  }
  class Reader extends TestTask {
    long result = 0;
    void test() {
      for(long i = 0; i < testCycles; i++)
        for(int index = 0; index < containerSize; index++)
          result += testContainer.get(index);
    }
    void putResults() {
      readResult += result;
      readTime += duration;
    }
  }
  class Writer extends TestTask {
    void test() {
      for(long i = 0; i < testCycles; i++)
        for(int index = 0; index < containerSize; index++)
          testContainer.set(index, writeData[index]);
    }
    void putResults() {
      writeTime += duration;
    }
  }
  void startReadersAndWriters() {
    for(int i = 0; i < nReaders; i++)
      exec.execute(new Reader());
    for(int i = 0; i < nWriters; i++)
      exec.execute(new Writer());
  }
}

class SynchronizedArrayListTest extends ListTest {
  List<Integer> containerInitializer() {
    return Collections.synchronizedList(
      new ArrayList<Integer>(
        new CountingIntegerList(containerSize)));
  }
  SynchronizedArrayListTest(int nReaders, int nWriters) {
    super("Synched ArrayList", nReaders, nWriters);
  }
}

class CopyOnWriteArrayListTest extends ListTest {
  List<Integer> containerInitializer() {
    return new CopyOnWriteArrayList<Integer>(
      new CountingIntegerList(containerSize));
  }
  CopyOnWriteArrayListTest(int nReaders, int nWriters) {
    super("CopyOnWriteArrayList", nReaders, nWriters);
  }
}

public class ListComparisons {
  public static void main(String[] args) {
    Tester.initMain(args);
    new SynchronizedArrayListTest(10, 0);
    new SynchronizedArrayListTest(9, 1);
    new SynchronizedArrayListTest(5, 5);
    new CopyOnWriteArrayListTest(10, 0);
    new CopyOnWriteArrayListTest(9, 1);
    new CopyOnWriteArrayListTest(5, 5);
    Tester.exec.shutdown();
  }
} /* Output: (Sample)
Type                             Read time     Write time
Synched ArrayList 10r 0w      232158294700              0
Synched ArrayList 9r 1w       198947618203    24918613399
readTime + writeTime =        223866231602
Synched ArrayList 5r 5w       117367305062   132176613508
readTime + writeTime =        249543918570
CopyOnWriteArrayList 10r 0w      758386889              0
CopyOnWriteArrayList 9r 1w       741305671      136145237
readTime + writeTime =           877450908
CopyOnWriteArrayList 5r 5w       212763075    67967464300
readTime + writeTime =         68180227375
JDK 1.8
Type                             Read time     Write time
Synched ArrayList 10r 0w        5653558783              0
Synched ArrayList 10r 0w        5461310535              0
Synched ArrayList 10r 0w        5450806521              0
Synched ArrayList 10r 0w        5484779594              0
Synched ArrayList 10r 0w        5819855118              0
Synched ArrayList 10r 0w        5299623253              0
Synched ArrayList 10r 0w        5206898618              0
Synched ArrayList 10r 0w        5216267256              0
Synched ArrayList 10r 0w        4024560818              0
Synched ArrayList 10r 0w        5197339252              0
Synched ArrayList 9r 1w         4472778863      586533757
readTime + writeTime =          5059312620
Synched ArrayList 9r 1w         5181755785      588902003
readTime + writeTime =          5770657788
Synched ArrayList 9r 1w         5486557705      606562495
readTime + writeTime =          6093120200
Synched ArrayList 9r 1w         5129432015      574345718
readTime + writeTime =          5703777733
Synched ArrayList 9r 1w         5506391441      603482236
readTime + writeTime =          6109873677
Synched ArrayList 9r 1w         5625473391      634067584
readTime + writeTime =          6259540975
Synched ArrayList 9r 1w         5320444400      589818854
readTime + writeTime =          5910263254
Synched ArrayList 9r 1w         5370611604      569553634
readTime + writeTime =          5940165238
Synched ArrayList 9r 1w         4923307417      534560220
readTime + writeTime =          5457867637
Synched ArrayList 9r 1w         5238845556      596172234
readTime + writeTime =          5835017790
Synched ArrayList 5r 5w         2793276715     2944530968
readTime + writeTime =          5737807683
Synched ArrayList 5r 5w         2836116814     2906217392
readTime + writeTime =          5742334206
Synched ArrayList 5r 5w         2943410564     2922671095
readTime + writeTime =          5866081659
Synched ArrayList 5r 5w         2870410188     2866681206
readTime + writeTime =          5737091394
Synched ArrayList 5r 5w         3106259730     3108176821
readTime + writeTime =          6214436551
Synched ArrayList 5r 5w         2809715879     2845761275
readTime + writeTime =          5655477154
Synched ArrayList 5r 5w         2930782059     2931205847
readTime + writeTime =          5861987906
Synched ArrayList 5r 5w         2849422265     2936742015
readTime + writeTime =          5786164280
Synched ArrayList 5r 5w         2812794427     2963829731
readTime + writeTime =          5776624158
Synched ArrayList 5r 5w         2972961890     3071935564
readTime + writeTime =          6044897454
CopyOnWriteArrayList 10r 0w      137294091              0
CopyOnWriteArrayList 10r 0w       93761653              0
CopyOnWriteArrayList 10r 0w      107751747              0
CopyOnWriteArrayList 10r 0w       92696840              0
CopyOnWriteArrayList 10r 0w      115716461              0
CopyOnWriteArrayList 10r 0w       78855561              0
CopyOnWriteArrayList 10r 0w      108389782              0
CopyOnWriteArrayList 10r 0w       85066540              0
CopyOnWriteArrayList 10r 0w      100505037              0
CopyOnWriteArrayList 10r 0w       86059082              0
CopyOnWriteArrayList 9r 1w        89600760       59670977
readTime + writeTime =           149271737
CopyOnWriteArrayList 9r 1w        76871335       43272010
readTime + writeTime =           120143345
CopyOnWriteArrayList 9r 1w        95119824       35010093
readTime + writeTime =           130129917
CopyOnWriteArrayList 9r 1w        75855270       34818084
readTime + writeTime =           110673354
CopyOnWriteArrayList 9r 1w        80257352       33418861
readTime + writeTime =           113676213
CopyOnWriteArrayList 9r 1w        81065582       36759549
readTime + writeTime =           117825131
CopyOnWriteArrayList 9r 1w        87805119       36393920
readTime + writeTime =           124199039
CopyOnWriteArrayList 9r 1w        88938352       35278647
readTime + writeTime =           124216999
CopyOnWriteArrayList 9r 1w        81253741       32550333
readTime + writeTime =           113804074
CopyOnWriteArrayList 9r 1w        77484561       34265151
readTime + writeTime =           111749712
CopyOnWriteArrayList 5r 5w        52180088     1452855323
readTime + writeTime =          1505035411
CopyOnWriteArrayList 5r 5w        88398248     1294023794
readTime + writeTime =          1382422042
CopyOnWriteArrayList 5r 5w        55590052     1313494467
readTime + writeTime =          1369084519
CopyOnWriteArrayList 5r 5w        56885360     1331962761
readTime + writeTime =          1388848121
CopyOnWriteArrayList 5r 5w        56070713     1267296146
readTime + writeTime =          1323366859
CopyOnWriteArrayList 5r 5w        49540290     1229936196
readTime + writeTime =          1279476486
CopyOnWriteArrayList 5r 5w        56181900     1341645286
readTime + writeTime =          1397827186
CopyOnWriteArrayList 5r 5w        67121243     1309027385
readTime + writeTime =          1376148628
CopyOnWriteArrayList 5r 5w        51951729     1312976173
readTime + writeTime =          1364927902
CopyOnWriteArrayList 5r 5w        49011733     1321935138
readTime + writeTime =          1370946871
*///:~
